package com.ruoyi.media.forestApi.common;

import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.annotation.BaseRequest;
import com.dtflys.forest.annotation.Body;
import com.dtflys.forest.annotation.Post;
import com.dtflys.forest.http.ForestResponse;
import com.ruoyi.media.forestApi.WechatMiniProApi;
import com.ruoyi.media.forestApi.vo.BaseResVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@RequiredArgsConstructor
@Component
public class WechatMiniProRequest {

  private final WechatMiniProApi wechatMiniProApi;

    @Value("${wxgzh.appId}")
    private String appId;

    @Value("${wxgzh.appSecret}")
    private String appSecret;


    /**
     * 获取小程序accessToken
     * @return
     */
    public BaseResVo getAccessToken(){
        Map<String,Object> param = new HashMap<>();
        param.put("grant_type","client_credential");
        param.put("appid",appId);
        param.put("secret",appSecret);
        param.put("force_refresh",true);
        String res = wechatMiniProApi.getAccessToken(param);
        return  JSONObject.toJavaObject(JSONObject.parseObject(res), BaseResVo.class);
    }



    /**
     * 获取微信用户openid和sessionKey
     * @return
     */
    public BaseResVo jscode2session(String code){
        String res =  wechatMiniProApi.jscode2session(appId,appSecret,code);
        return  JSONObject.toJavaObject(JSONObject.parseObject(res), BaseResVo.class);
    }


    /**
     * 生成带参数的小程序码
     * @param accessToken
     * @return
     */
    public ForestResponse wxParamQRCode(String accessToken, String scene){
        Map<String,Object> param =  new HashMap<>();
        param.put("page","pages/index/index");
        param.put("scene","id="+scene);
        param.put("check_path",true);
        param.put("env_version","trial");
        //param.put("env_version","develop");
        return  wechatMiniProApi.wxParamQRCode(param,accessToken);
    }





}
